/**
 * @filename:StkweightServiceImpl Jan 23, 2025
 * @project Wonder Vue Admin Api  1.1.0
 * Copyright(c) 2022 Wonder Co. Ltd. 
 * All right reserved. 
 */
package com.wonder.app.service.impl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.vtuzx.core.bean.dto.VtuzxMap;
import com.vtuzx.core.utils.VtuzxUtil;
import com.wonder.admin.service.IVtuzxKingdeeService;
import com.wonder.app.domain.*;
import com.wonder.app.mapper.*;
import com.wonder.app.service.*;
import com.wonder.app.request.StkweightRequest;
import com.wonder.bean.kingdee.VtuzxKingdeeCloudClient;
import com.wonder.bean.kingdee.xqc.VtuzxArrayList;
import com.wonder.common.utils.StringUtils;
import com.wonder.common.utils.MessageUtils;
import com.wonder.common.common.AjaxResult;

import java.util.*;

import com.wonder.constant.BillType;
import com.wonder.constant.IXqcConst;
import org.bson.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**   
 * @Description:TODO(称重申请单服务实现)
 *
 * @version: 1.1.0
 * @author: Wonder
 * 
 */
@Service
public class StkweightServiceImpl  extends ServiceImpl<StkweightDao, Stkweight> implements StkweightService  {
	/** 日志 */
	private static final Logger _logger = LoggerFactory.getLogger(StkweightServiceImpl.class);

	@Autowired
	private StkweightService stkweightService;

	@Autowired
	private YzjService yzjService;

	@Autowired
	private OtherinspectService otherinspectService;

	@Autowired
	private OtherInspectDetailService otherInspectDetailService;

	@Autowired
	private IVtuzxKingdeeService vtuzxKingdeeService;
	@Autowired
	private StkweightDao stkweightDao;
	@Autowired
	private StkweightDetailService stkweightDetailService;
	@Autowired
	private StkweightDetailDao stkweightDetailDao;
	@Autowired
	private MaterialService materialService;
	@Autowired
	private MaterialDao materialDao;
	@Autowired
	private PurReturnService purReturnService;
	@Autowired
	private PurReturnDao purReturnDao;
	@Autowired
	private PurReturnDetailService purReturnDetailService;
	@Autowired
	private PurReturnDetailDao purReturnDetailDao;
	@Autowired
	private StkotheroutService stkotheroutService;
	@Autowired
	private StkotheroutDao stkotheroutDao;
	@Autowired
	private StkOtheroutDetailService stkOtheroutDetailService;
	@Autowired
	private StkOtheroutDetailDao stkOtheroutDetailDao;
	@Autowired
	private StkOtherInService stkOtherInService;
	@Autowired
	private StkOtherInDao stkOtherInDao;
	@Autowired
	private StkOtherInDeatilService stkOtherInDeatilService;
	@Autowired
	private StkOtherInDeatilDao stkOtherInDeatilDao;
    /**
     * 验证称重申请单信息
     * 
     * @param stkweight
     * @param type
     * @return String
     */
    @Override
    public String validate(Stkweight stkweight,String type){
    
    
       return "";
    }
    
    
    /**
	 * Description:称重申请单分页返回
	 * @param request //检索条件
	 * @param page //分页参数
	 * @return IPage<Stkweight>   //分页记录
	 */
	@Override 
	public IPage<Stkweight> selectList(StkweightRequest request, Page<Stkweight> page){
	    LambdaQueryWrapper<Stkweight> lambdaQueryWrapper = new LambdaQueryWrapper<>();
	    
	    if (null != request.getId()) {
	       lambdaQueryWrapper.eq(Stkweight::getId, request.getId());
	      }
		if (StringUtils.hasText(request.getBillNo())) {
			lambdaQueryWrapper.like(Stkweight::getBillNo, request.getBillNo());
		}

		if (StringUtils.hasText(request.getDepartmentName())) {
			lambdaQueryWrapper.like(Stkweight::getDepartmentName, request.getDepartmentName());
		}

		if (StringUtils.hasText(request.getUser())) {
			lambdaQueryWrapper.like(Stkweight::getUser, request.getUser());
		}

		if (StringUtils.hasText(request.getPurOrderNo())) {
			lambdaQueryWrapper.like(Stkweight::getPurOrderNo, request.getPurOrderNo());
		}

		if (request.getDate() != null) {
			Date[] fCREATEDATE = request.getDate();
			if (fCREATEDATE.length > 0) {
				// 设置开始时间为当天的零点
				Calendar startCalendar = Calendar.getInstance();
				startCalendar.setTime(fCREATEDATE[0]);
				startCalendar.set(Calendar.HOUR_OF_DAY, 0);
				startCalendar.set(Calendar.MINUTE, 0);
				startCalendar.set(Calendar.SECOND, 0);
				startCalendar.set(Calendar.MILLISECOND, 0);
				Date startDate = startCalendar.getTime();
				lambdaQueryWrapper.ge(Stkweight::getDATE, startDate);
			}
			if (fCREATEDATE.length == 2) {
				// 设置结束时间为当天的24点
				Calendar endCalendar = Calendar.getInstance();
				endCalendar.setTime(fCREATEDATE[1]);
				endCalendar.set(Calendar.HOUR_OF_DAY, 23);
				endCalendar.set(Calendar.MINUTE, 59);
				endCalendar.set(Calendar.SECOND, 59);
				endCalendar.set(Calendar.MILLISECOND, 999);
				Date endDate = endCalendar.getTime();
				lambdaQueryWrapper.le(Stkweight::getDATE, endDate);
			}
		}
        
		lambdaQueryWrapper.orderByDesc(Stkweight::getId);	
		Page<Stkweight> selectPage = this.baseMapper.selectPage(page, lambdaQueryWrapper);
		return selectPage;
    }


	/**
	 * 称重申请第一次称重
	 * @param bh
	 * @return
	 */
	@Override
	public Boolean firStkWeight(String bh){
		// 根据bh查询是否有数据
		QueryWrapper<Stkweight> wrapper = new QueryWrapper<>();
		wrapper.eq("Fid", bh);
		Stkweight stkweight =stkweightDao.selectOne(wrapper);
		if (stkweight != null){
			return  true;
		}else {
			return  false;
		}
	}


	/**
	 * 称重申请第二次接口
	 * @param bh
	 * @param sl
	 * @return
	 */
	@Override
	public VtuzxMap stkSecWeight(String bh, String sl) throws Exception {
		//解析Map集合
		String Fid = bh;//标识
		String BillNo = "";
		String materialNo = "";
		String type = "";
		double ydqty = 0;
		double slqty = 0;
		String supperName = "";
		String supperNo = "";
		// 根据bh查询是否有数据
		QueryWrapper<Stkweight> wrapper = new QueryWrapper<>();
		wrapper.eq("Fid", bh);
		Stkweight stkweight =stkweightDao.selectOne(wrapper);
		if (stkweight != null) {
			BillNo = stkweight.getBillNo();
			type = stkweight.getType();
		}
		QueryWrapper<StkweightDetail> stkweightDetailQueryWrapper = new QueryWrapper<>();
		stkweightDetailQueryWrapper.eq("BillNo",BillNo);
		StkweightDetail stkweightDetail = stkweightDetailDao.selectOne(stkweightDetailQueryWrapper);
		if (stkweightDetail != null) {
			materialNo = stkweightDetail.getMaterialNo();
			ydqty = stkweightDetail.getBasicQty();
			slqty = stkweightDetail.getWeightQty();
		}
		Double weight = Double.parseDouble(sl.toString());//重量//查询物料换算关系计算吨数转换后数量
		double conversion = 1;
		VtuzxKingdeeCloudClient client = vtuzxKingdeeService.loginKingdee();
		//根据物料编码查询转换比
		List<VtuzxMap> prdInQuery = vtuzxKingdeeService.query(client, "BD_MATERIAL", new VtuzxArrayList<>(
						"F_PSUH_Decimal_re5"
				),
				String.format("fnumber = '%s'", materialNo),null , 0, 0);
		if (!VtuzxUtil.isEmpty(prdInQuery)) {
			conversion = VtuzxUtil.getDouble(prdInQuery.get(0),"F_PSUH_Decimal_re5");
			if (conversion == 0){
				conversion = 1;
			}

		} else {
			throw new RuntimeException(String.format("金蝶不存在物料%s",materialNo));
		}

		//计算转换后数量 + 已收料数量
		Double conweight = weight * conversion + slqty;
		Double bcweight = weight * conversion;
		if (type.equals("A")) {
			//根据物料编码查询转换比
			List<VtuzxMap> purorderQuery = vtuzxKingdeeService.query(client, "PUR_PurchaseOrder", new VtuzxArrayList<>(
							"FSupplierId.fname",
							"FSupplierId.fnumber"
					),
					String.format("FBillNo = '%s'", stkweight.getPurOrderNo()), null, 0, 0);
			if (!VtuzxUtil.isEmpty(purorderQuery)) {
				supperName = VtuzxUtil.getString(purorderQuery.get(0), "FSupplierId.fname");
				supperNo = VtuzxUtil.getString(purorderQuery.get(0),"FSupplierId.fnumber");
			} else {
				throw new RuntimeException(String.format("金蝶不存在供应商%s", stkweight.getPurOrderNo()));
			}
		}
		//异常处理数据
		VtuzxMap abmap = new VtuzxMap();
		abmap.append("BillNo",BillNo)
				.append("SourceQty",ydqty)
				.append("zqty",conweight)
				.append("AbnormalQty",bcweight)
				.append("DepartmentNo",stkweight.getDepartmentNo())
				.append("DepartmentName",stkweight.getDepartmentName())
				.append("PurOrderNo",stkweight.getPurOrderNo())
				.append("Note",stkweight.getNote())
				.append("MaterialNo",stkweightDetail.getMaterialNo())
				.append("MaterialName",stkweightDetail.getMaterialName())
				.append("Specification",stkweightDetail.getSpecification())
				.append("BasicUnitNo",stkweightDetail.getBasicUnitNo())
				.append("BasicUnitName",stkweightDetail.getBasicUnitName())
				.append("Lot",stkweightDetail.getLot())
				.append("StockNo",stkweightDetail.getStockNo())
				.append("StockName",stkweightDetail.getStockName())
				.append("LocationNo",stkweightDetail.getLocationNo())
				.append("LocationName",stkweightDetail.getLocationName())
				.append("BasicQty",stkweightDetail.getBasicQty())
				.append("AssistantUnitNo",stkweightDetail.getAssistantUnitNo())
				.append("AssistantUnitName",stkweightDetail.getAssistantUnitName())
				.append("ReturnMethod",stkweightDetail.getReturnMethod())
				.append("SupperNo",supperNo)
				.append("SupperName",supperName);
		VtuzxMap resmap = new VtuzxMap();
		//A为采购退料
		if (type.equals("A")){
			resmap = savePurReturn(abmap,bh,sl);
			//修改称重数量
			stkweightDetail.setWeightQty(conweight);
			stkweightDetailService.updateById(stkweightDetail);
			//修改金蝶称重申请单的重量

		} else if (type.equals("B")) {//B为其他入库(其他入库先生成检验单)
			resmap =  saveOtherInspect(abmap,bh,sl);
			stkweightDetail.setWeightQty(conweight);
			stkweightDetailService.updateById(stkweightDetail);
		}else {//除了AB其他为其他出库
			resmap = saveOtherOut(abmap,bh,sl);
			stkweightDetail.setWeightQty(conweight);
			stkweightDetailService.updateById(stkweightDetail);
		}
		try {
			//修改金蝶称重申请数量
			updateWeightCount(stkweight,stkweightDetail);
		} catch (Exception e){
//			throw new RuntimeException(e.getMessage());
			_logger.info(e.getMessage());
		}


		return resmap;
	}

	/**
	 * 修改金蝶称重申请数量
	 */
	private void updateWeightCount(Stkweight stkweight,StkweightDetail stkweightDetail) throws Exception{
		Map<String, Object> model = new LinkedHashMap<>();//总参
		model.put("Fid",stkweight.getFID());
		List<Map<String, Object>> OtherInEntry = new ArrayList<>();//表体体数据
		Map<String, Object> colMap = new LinkedHashMap<>();
		colMap.put("FEntryID",stkweightDetail.getEntryId());//明细id
		colMap.put("F_PSUH_Qty_tzk",stkweightDetail.getWeightQty());//称重数量
		OtherInEntry.add(colMap);
		model.put("F_PSUH_Entity_3iy",OtherInEntry);//表体
		//固定参数
		Map<String, Object> kneeParam = new LinkedHashMap<>();
		List<String>  NeedUpDateFields = new ArrayList<>();
		NeedUpDateFields.add("F_PSUH_Qty_tzk");
		kneeParam.put("NeedUpDateFields",NeedUpDateFields);
		kneeParam.put("NeedReturnFields", new ArrayList<>());
		kneeParam.put("IsDeleteEntry", true);
		kneeParam.put("SubSystemId", "");
		kneeParam.put("IsVerifyBaseDataField", true);
		kneeParam.put("IsEntryBatchFill", false);
		kneeParam.put("ValidateFlag", true);
		kneeParam.put("NumberSearch", true);
		kneeParam.put("InterationFlags", "");
		kneeParam.put("Model", model);
		//金蝶登录
		VtuzxKingdeeCloudClient client = vtuzxKingdeeService.loginKingdee();
		try {
			List<VtuzxMap> customerList = vtuzxKingdeeService.save(client,"PSUH_CZSQD",kneeParam);

		} catch (Exception e){

			throw new RuntimeException(e.getMessage());
		}
	}

	/**
	 * 采购退料单
	 * @param abmap
	 * @return
	 */
	private VtuzxMap savePurReturn(VtuzxMap abmap,String bh,String sl){
		String BillNo = IXqcConst.createBillNo(BillType.CGTH);
		PurReturn purReturn = new PurReturn();
		PurReturnDetail purReturnDetail = new PurReturnDetail();
		purReturn.setBillNo(BillNo);
		purReturn.setWeightNo(abmap.getString("BillNo"));
		purReturn.setReturnDate(new Date());
		purReturn.setReturnMethod(abmap.getString("ReturnMethod"));//退料方式A退料补料B退料并扣款
		purReturn.setOrgNo("100");//先固定传100
		purReturn.setOrgName("大连德原工业有限公司");
		purReturn.setSupplierNo(abmap.getString("SupperNo"));
		purReturn.setSupplierName(abmap.getString("SupperName"));
		purReturn.setStatus("save");//审核状态A未审核、B已审核
		purReturn.setPurBillNo(abmap.getString("PurOrderNo"));

		purReturnDetail.setMaterialNo(abmap.getString("MaterialNo"));
		purReturnDetail.setMaterialName(abmap.getString("MaterialName"));
		purReturnDetail.setSpecification(abmap.getString("Specification"));
		purReturnDetail.setBasicUnitNo(abmap.getString("BasicUnitNo"));
		purReturnDetail.setBasicUnitName(abmap.getString("BasicUnitName"));
		purReturnDetail.setLot(abmap.getString("Lot"));
		purReturnDetail.setInfactQty(abmap.getDouble("AbnormalQty"));
		purReturnDetail.setStockNo(abmap.getString("StockNo"));
		purReturnDetail.setStockName(abmap.getString("StockName"));
		purReturnDetail.setLocationnNo(abmap.getString("LocationNo"));
		purReturnDetail.setLocationName(abmap.getString("LocationName"));
		purReturnDetail.setBillNo(BillNo);

		purReturnService.saveOrUpdate(purReturn);
		boolean purdetail =  purReturnDetailService.saveOrUpdate(purReturnDetail);
		VtuzxMap result = new VtuzxMap();
		String msg = "action=save&bh="+bh+"&sl="+sl;
		result.append("result",purdetail)
				.append("BillNo",BillNo)
				.append("bh",bh)
				.append("msg",msg);
		//给库管发送待办通知
		try {
			Map<String,Object> data = new HashMap<>();
			data.put("billNo",BillNo);
			yzjService.sendMsgToWare(data,BillType.CGTH.label());
		} catch (Exception e){
			_logger.info(e.getMessage());
		}
		return result;
	}


	/**
	 * 其他入库单
	 * @param abmap
	 * @return
	 */
	private VtuzxMap saveOtherInspect(VtuzxMap abmap,String bh,String sl){
		String MaterialName = abmap.getString("MaterialName");
		//收缩膜只把信息传回称重申请单上，做记录，不生成其他入库单
		if("收缩膜".equals(MaterialName)){
			String msg = "action=save&bh="+bh+"&sl="+sl;
			VtuzxMap result = new VtuzxMap();
			result.append("result",true)
					.append("BillNo","")
					.append("bh",bh)
					.append("msg",msg);
			return result;
		}

		String BillNo = IXqcConst.createBillNo(BillType.LLJY);
		Otherinspect otherinspect = new Otherinspect();
		OtherInspectDetail otherInspectDetail = new OtherInspectDetail();
		//其他入库检验单单据头
		otherinspect.setBillNo(BillNo);
		otherinspect.setOrgNo("100");
		otherinspect.setDeptNumber(abmap.getString("DepartmentNo"));//部门编码
		otherinspect.setOrgName("大连德原工业有限公司");
		otherinspect.setWeightNo(abmap.getString("BillNo"));
		otherinspect.setCreateTime(new Date());
		otherinspect.setStatus("save");
		otherinspect.setCreateUserName("称重");

		//拼写其他入库单详细
		otherInspectDetail.setBillNo(BillNo);
		otherInspectDetail.setMaterialNo(abmap.getString("MaterialNo"));
		otherInspectDetail.setMaterialName(abmap.getString("MaterialName"));
		otherInspectDetail.setSpecification(abmap.getString("Specification"));
		otherInspectDetail.setUnitNo(abmap.getString("BasicUnitNo"));
		otherInspectDetail.setUnitName(abmap.getString("BasicUnitName"));
		otherInspectDetail.setFlot(abmap.getString("Lot"));
		otherInspectDetail.setInspectQty(abmap.getDouble("AbnormalQty"));//称重数量
		otherInspectDetail.setQualifiedQty(abmap.getDouble("AbnormalQty"));//合格数量
		otherInspectDetail.setStockNo(abmap.getString("StockNo"));
		otherInspectDetail.setStockName(abmap.getString("StockName"));
		otherInspectDetail.setLocationNo(abmap.getString("LocationNo"));
		otherInspectDetail.setLocationName(abmap.getString("LocationName"));
		otherInspectDetail.setResult("未检");
		otherInspectDetail.setWeightNo(abmap.getString("BillNo"));

		//保存其他入库检验单
		boolean res =  otherinspectService.saveOrUpdate(otherinspect);
		otherInspectDetailService.saveOrUpdate(otherInspectDetail);
		String msg = "action=save&bh="+bh+"&sl="+sl;
		VtuzxMap result = new VtuzxMap();
		result.append("result",res)
				.append("BillNo",BillNo)
				.append("bh",bh)
				.append("msg",msg);
		//给质检部发送通知
		try {
			Map<String,Object> data = new HashMap<>();
			data.put("billNo",BillNo);
			yzjService.sendMsgToQuality(data,BillType.LLJY.label());
		} catch (Exception e){
			_logger.info(e.getMessage());
		}
		return result;
	}


	/**
	 * 其他入库单
	 * @param abmap
	 * @return
	 */
	private VtuzxMap saveOtherIn(VtuzxMap abmap,String bh,String sl){
		String BillNo = IXqcConst.createBillNo(BillType.QTRK);
		StkOtherIn stkOtherIn = new StkOtherIn();
		StkOtherInDeatil stkOtherInDeatil = new StkOtherInDeatil();
		//其他入库单单据头
		stkOtherIn.setBillNo(BillNo);
		stkOtherIn.setOrgNo("100");
		stkOtherIn.setDeptNumber(abmap.getString("DepartmentNo"));//部门编码
		stkOtherIn.setOrgName("大连德原工业有限公司");
		stkOtherIn.setWeightNo(abmap.getString("BillNo"));
		stkOtherIn.setDate(new Date());
		stkOtherIn.setStatus("save");

		//拼写其他入库单详细
		stkOtherInDeatil.setBillNo(BillNo);
		stkOtherInDeatil.setMaterialNo(abmap.getString("MaterialNo"));
		stkOtherInDeatil.setMaterialName(abmap.getString("MaterialName"));
		stkOtherInDeatil.setSpecification(abmap.getString("Specification"));
		stkOtherInDeatil.setBasicUnitNo(abmap.getString("BasicUnitNo"));
		stkOtherInDeatil.setBasicUnitName(abmap.getString("BasicUnitName"));
		stkOtherInDeatil.setLot(abmap.getString("Lot"));
		stkOtherInDeatil.setQty(abmap.getDouble("AbnormalQty"));
		stkOtherInDeatil.setStockNo(abmap.getString("StockNo"));
		stkOtherInDeatil.setStockName(abmap.getString("StockName"));
		stkOtherInDeatil.setLocationNo(abmap.getString("LocationNo"));
		stkOtherInDeatil.setLocationName(abmap.getString("LocationName"));
		stkOtherInDeatil.setNote(abmap.getString("Note"));
		//保存其他入库单
		boolean res =  stkOtherInService.saveOrUpdate(stkOtherIn);
		stkOtherInDeatilService.saveOrUpdate(stkOtherInDeatil);
		String msg = "action=save&bh="+bh+"&sl="+sl;
		VtuzxMap result = new VtuzxMap();
		result.append("result",res)
				.append("BillNo",BillNo)
				.append("bh",bh)
				.append("msg",msg);
		return result;
	}

	/**
	 * 其他出库单
	 * @param abmap
	 * @return
	 */
	private VtuzxMap saveOtherOut(VtuzxMap abmap,String bh,String sl){
		//先不生成其他出库单，暂时关闭此功能
		String msg = "action=save&bh="+bh+"&sl="+sl;
		VtuzxMap result = new VtuzxMap();
		result.append("result",true)
				.append("BillNo","")
				.append("bh",bh)
				.append("msg",msg);
//		String BillNo = IXqcConst.createBillNo(BillType.QTCK);
//		Stkotherout stkotherout = new Stkotherout();
//		StkOtheroutDetail stkOtheroutDetail = new StkOtheroutDetail();
//
//		stkotherout.setBillNo(BillNo);
//		stkotherout.setDate(new Date());
//		stkotherout.setDeptNumber(abmap.getString("DepartmentNo"));
//		stkotherout.setWeightNo(abmap.getString("BillNo"));
//		stkotherout.setStatus("save");
//
//		stkOtheroutDetail.setBillNo(BillNo);
//		stkOtheroutDetail.setMaterialNo(abmap.getString("MaterialNo"));
//		stkOtheroutDetail.setMaterialName(abmap.getString("MaterialName"));
//		stkOtheroutDetail.setSpecification(abmap.getString("Specification"));
//		stkOtheroutDetail.setBasicUnitNo(abmap.getString("BasicUnitNo"));
//		stkOtheroutDetail.setBasicUnitName(abmap.getString("BasicUnitName"));
//		stkOtheroutDetail.setLot(abmap.getString("Lot"));
//		stkOtheroutDetail.setQty(abmap.getDouble("zqty"));
//		stkOtheroutDetail.setStockNo(abmap.getString("StockNo"));
//		stkOtheroutDetail.setStockName(abmap.getString("StockName"));
//		stkOtheroutDetail.setLocationNo(abmap.getString("LocationNo"));
//		stkOtheroutDetail.setLocationName(abmap.getString("LocationName"));
//
//		//保存其他出库单
//		boolean res = stkotheroutService.saveOrUpdate(stkotherout);
//		boolean resdetail = stkOtheroutDetailService.saveOrUpdate(stkOtheroutDetail);
//		String msg = "action=save&bh="+bh+"&sl="+sl;
//		VtuzxMap result = new VtuzxMap();
//		result.append("result",res)
//				.append("BillNo",BillNo)
//				.append("bh",bh)
//				.append("msg",msg);
//		//给库管发送待办通知
//		try {
//			Map<String,Object> data = new HashMap<>();
//			data.put("billNo",BillNo);
//			yzjService.sendMsgToWare(data,BillType.QTCK.label());
//		} catch (Exception e){
//			_logger.info(e.getMessage());
//		}
		return result;
	}

	/**
	 * receive详情
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@Override
	public Map<String, Object> getStkWeightDetail(Map<String, Object> map) {
		String billNo = map.get("billNo").toString();
		Stkweight stkweightform = stkweightDao.selectOne(new LambdaQueryWrapper<Stkweight>()
				.eq(Stkweight::getBillNo,billNo));
		List<StkweightDetail> stkweightList = stkweightDetailDao.selectList(new LambdaQueryWrapper<StkweightDetail>()
				.eq(StkweightDetail::getBillNo,billNo));
		Map<String,Object> data = new HashMap<>();
		data.put("data",stkweightform);
		data.put("dataList",stkweightList);
		return data;
	}

	/**
	 * 审核称重申请单
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@Override
	public boolean audit(JSONObject map) throws Exception {
		_logger.info("审核称重申请单"+ map.toString());
		JSONArray erceives = map.getJSONArray("WeightList");
		JSONObject receiveObj = (JSONObject) erceives.get(0);
		JSONArray head = receiveObj.getJSONArray("model");//单据头数据
		JSONArray detailList = receiveObj.getJSONArray("detail");//单据体数据、
		List<Stkweight> stkweightList = new ArrayList<>();
		List<StkweightDetail> stkweightDetailList = new ArrayList<>();
		List<Long> fidList = new ArrayList<>();//单据头需要删除的fid集合
		List<Long> entryIdList = new ArrayList<>();//单据体需要删除的entryid集合
		for (Object obj : head) {
			JSONObject jsonObject = (JSONObject) obj;
			//将json直接转成实体类
			Stkweight stkweight = jsonObject.toBean(Stkweight.class);
			Long fid = stkweight.getFID();
			if (!fidList.contains(fid)){
				fidList.add(fid);
			}
			stkweightList.add(stkweight);
		}
		//单据体数据
		for (Object obj : detailList) {
			JSONObject jsonObject = (JSONObject) obj;
			//将json直接转成实体类
			StkweightDetail stkweightDetail = jsonObject.toBean(StkweightDetail.class);
			stkweightDetail.setWeightQty(0.0);
			if (Double.parseDouble(stkweightDetail.getLocationNo()) == 0){
				stkweightDetail.setLocationNo("");
			}
			Long entryId = stkweightDetail.getEntryId();
			if (!entryIdList.contains(entryId)){
				entryIdList.add(entryId);
			}
			stkweightDetailList.add(stkweightDetail);
		}
		//查询有没有需要删除的单据
		List<Stkweight> stkweights =  stkweightDao.selectList(new LambdaQueryWrapper<Stkweight>()
				.in(Stkweight::getFID,fidList));
		if (null != stkweights || stkweights.size() > 0){
			stkweightDao.delete(new LambdaQueryWrapper<Stkweight>()
					.in(Stkweight::getFID,fidList));

		}
		List<StkweightDetail> stkweightDetails =  stkweightDetailDao.selectList(new LambdaQueryWrapper<StkweightDetail>()
				.in(StkweightDetail::getEntryId,entryIdList));
		if (null != stkweightDetails || stkweightDetails.size() > 0){
			stkweightDetailDao.delete(new LambdaQueryWrapper<StkweightDetail>()
					.in(com.wonder.app.domain.StkweightDetail::getEntryId,entryIdList));

		}
		//向数据库添加数据
		boolean resHead = stkweightService.saveOrUpdateBatch(stkweightList);
		boolean res = stkweightDetailService.saveOrUpdateBatch(stkweightDetailList);
		return res && resHead;
	}

	/**
	 * 金蝶反审称重申请单
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@Override
	public boolean unAudit(JSONObject map) throws Exception {
		JSONArray ReceiveList = map.getJSONArray("WeightList");
		List<String> billNoList = ReceiveList.toList(String.class);
		//先删除采购退，其他入，其他出
		List<String> otherOutNoList = new ArrayList<>();//其他出库需要删除的
		List<String> purReturnNoList = new ArrayList<>();//采购退货需要删除的单据
		List<String> otherInNoList = new ArrayList<>();//其他入库需要删除的单据
		//采购退
		List<PurReturn> returnList = purReturnDao.selectList(new LambdaQueryWrapper<PurReturn>()
				.in(PurReturn::getWeightNo, billNoList));
		if(returnList.size() > 0){
			for (PurReturn purReturn : returnList){
				String returnNo = purReturn.getBillNo();
				if (!purReturnNoList.contains(returnNo)){
					purReturnNoList.add(returnNo);
				}
			}
			boolean remove = purReturnService.remove(new LambdaQueryWrapper<PurReturn>()
					.in(PurReturn::getBillNo, purReturnNoList));
			boolean detailRes =purReturnDetailService.remove(new LambdaQueryWrapper<PurReturnDetail>()
					.in(PurReturnDetail::getBillNo, purReturnNoList));
		}

		//其他出
		List<Stkotherout> stkotheroutList = stkotheroutDao.selectList(new LambdaQueryWrapper<Stkotherout>()
				.in(Stkotherout::getWeightNo, billNoList));
		if(stkotheroutList.size() > 0){
			for (Stkotherout stkotherout : stkotheroutList){
				String otherOutNo = stkotherout.getBillNo();
				if (!otherOutNoList.contains(otherOutNo)){
					otherOutNoList.add(otherOutNo);
				}
			}
			boolean remove = stkotheroutService.remove(new LambdaQueryWrapper<Stkotherout>()
					.in(Stkotherout::getBillNo, otherOutNoList));
			boolean detailRes =stkOtheroutDetailService.remove(new LambdaQueryWrapper<StkOtheroutDetail>()
					.in(StkOtheroutDetail::getBillNo, otherOutNoList));
		}

		//其他入
		List<StkOtherIn> stkOtherInList = stkOtherInDao.selectList(new LambdaQueryWrapper<StkOtherIn>()
				.in(StkOtherIn::getWeightNo, billNoList));
		if(stkOtherInList.size() > 0){
			for (StkOtherIn stkOtherIn : stkOtherInList){
				String otherinNo = stkOtherIn.getBillNo();
				if (!otherInNoList.contains(otherinNo)){
					otherInNoList.add(otherinNo);
				}
			}
			boolean remove = stkOtherInService.remove(new LambdaQueryWrapper<StkOtherIn>()
					.in(StkOtherIn::getBillNo, otherInNoList));
			boolean detailRes =stkOtherInDeatilService.remove(new LambdaQueryWrapper<StkOtherInDeatil>()
					.in(StkOtherInDeatil::getBillNo, otherInNoList));
		}
		//删除称重申请单
		boolean remove = stkweightService.remove(new LambdaQueryWrapper<Stkweight>()
				.in(Stkweight::getBillNo, billNoList));
		boolean detailRes = stkweightDetailService.remove(new LambdaQueryWrapper<StkweightDetail>()
				.in(StkweightDetail::getBillNo, billNoList));
		return remove && detailRes;
	}

}